package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.RetailImmunizationRecordStatus;
import com.jiantong.work.impl.dao.RetailImmunizationRecordDao;
import com.jiantong.work.model.RetailImmunizationRecord;
import com.jiantong.work.service.RetailImmunizationRecordReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by mz on 18/9/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class RetailImmunizationRecordReadServiceImpl implements RetailImmunizationRecordReadService{

    private final RetailImmunizationRecordDao retailImmunizationRecordDao;

    @Autowired
    public RetailImmunizationRecordReadServiceImpl(RetailImmunizationRecordDao retailImmunizationRecordDao) {
        this.retailImmunizationRecordDao = retailImmunizationRecordDao;
    }

    /**
     * 根据单据Id查询当前流程状态
     *
     * @param id 主键id
     * @return Map<String ,Integer>
     */
    @Override
    public Response<Map<String ,Integer>> findStatusById(Long id) {
        try{
            Integer status = retailImmunizationRecordDao.findStatusById(id);
            if(status == null){
                log.error("status(id={}) is not found", id);
                return Response.fail("status.not.found");
            }
            Map<String ,Integer> map = Maps.newHashMap();
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("failed to find status by id , case = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find status by id");
        }
    }

    /**
     * 根据ID查询单据的详情
     *
     * @param id 主键ID
     * @return RetailImmunizationRecord
     */
    @Override
    public Response<RetailImmunizationRecord> findById(Long id) {
        try{
            return Response.ok(retailImmunizationRecordDao.findById(id));
        }catch (Exception e){
            log.error("failed to find by id , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find by id");
        }
    }

    /**
     * 单个养殖场的散户档案分页查询
     *
     * @param pageNo         页码
     * @param pageSize       条数
     * @param breedFactoryId 养殖场ID
     * @param status         状态
     * @return Paging<RetailImmunizationRecord>
     */
    @Override
    public Response<Paging<RetailImmunizationRecord>> paging(Integer pageNo, Integer pageSize,
                                                             Long breedFactoryId, Integer status) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String , Object> criteria = Maps.newHashMap();

            if(status != null) criteria.put("status",status);

            criteria.put("breedFactoryId",breedFactoryId);

            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());

            return Response.ok(retailImmunizationRecordDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging for a factory , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to paging for a factory");
        }
    }

    /**
     * 分页查询多个散户养殖场的 免疫记录情况
     *
     * @param pageNo         页码
     * @param pageSize       条数
     * @param start 从时间
     * @param end 到时间
     * @param breedFactoryId 养殖场ID
     * @param status         状态
     * @return Paging<RetailImmunizationRecord>
     */
    @Override
    public Response<Paging<RetailImmunizationRecord>> pagingForMulti(Integer pageNo, Integer pageSize,
                                                                     Date start, Date end,
                                                                     Integer breedType , Long villageId,
                                                                     Long breedFactoryId, Integer status) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String , Object> criteria = Maps.newHashMap();

            if(status != null) criteria.put("status",status);

            if(start != null) criteria.put("start",start);

            if(end != null) criteria.put("end",end);

            List<Long> breedFactoryIds = new ArrayList<>();

            if(breedFactoryId != null){
                breedFactoryIds.add(breedFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            criteria.put("breedFactoryIds",breedFactoryIds);

            if(breedType != null) criteria.put("breedType",breedType);

            if(villageId != null) criteria.put("villageId",villageId);

            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());

            return Response.ok(retailImmunizationRecordDao.pagingMulti(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging for a factory , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to paging for a factory");
        }
    }

    /**
     * 查询出一个是否免疫的list
     *
     * @param breedFactoryIds 养殖场ids
     * @return List<Boolean>
     */
    @Override
    public Response<List<Boolean>> countIsVaccine(List<Long> breedFactoryIds) {
        try{
            return Response.ok(retailImmunizationRecordDao.countIsVaccine(breedFactoryIds));
        }catch (Exception e){
            log.error("failed to count is vaccine case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to count is vaccine");
        }
    }

    /**
     * 溯源
     *
     * @param breedFactoryId 养殖场id
     * @param markNumber     耳标号（完整）
     * @return ImmunizationRecord
     */
    @Override
    public Response<RetailImmunizationRecord> findByEarNumber(Long breedFactoryId, String markNumber) {
        try{
            Map<String , Object> criteria = Maps.newHashMap();
            criteria.put("breedFactoryId",breedFactoryId);
            criteria.put("markNumber",markNumber);
            criteria.put("status", RetailImmunizationRecordStatus.FINISHED.value());
            criteria.put("deleteFlag",DocumentsDeleteFlag.NORMAL.value());

            return Response.ok(retailImmunizationRecordDao.findByEarNumber(criteria));
        }catch (Exception e){
            log.error("failed to find this report by markNumber , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find this report by markNumber");
        }
    }
}
